import {request} from "../../request/index.js";
Page({
    data:{
        tabs:[
            {
                id:0,
                name:"全部",
                isActive:true
            },
            {
                id:1,
                name:"待付款",
                isActive:false
            },
            {
                id:2,
                name:"待收货",
                isActive:false
            },
            {
                id:3,
                name:"退货/退款",
                isActive:false
            }
        ]
    },
    onShow(){
        const token=wx.getStorageSync("token");
        if(!token){
            wx.navigateTo({
                url:"/pages/auth/index"
            });
        }else{
            let pages=getCurrentPages();
            let currentPage=pages[pages.length-1];
            console.log(currentPage);
            const {type}=currentPage.options;
            this.getCurrentIndex(type-1);
            this.getOrders(type);
        };
    },
    //请求订单事件监听
    async getOrders(type){
        const res=await request({url:"/my/orders/all",data:{type}});
        console.log(res);
    },
    //点击不同效果进来对应的当前tab页
    getCurrentIndex(index){
        this.data.tabs.forEach((item)=>{
            if(item.id===index){
                item.isActive=true;
            }else{
                item.isActive=false;
            };
        });
        // this.data.tabs.forEach(item=>item.id===e.detail.index?item.isActive=true:item.isActive=false);
        this.setData({
            tabs:this.data.tabs
        });
    },
     //点击子组件触发的父组件上的自定义事件
    clickChildTab(e){
        this.getCurrentIndex(e.detail.index);
        this.getOrders(e.detail.index+1);
    }
})